/**
 * 校验控件类型是否可以添加
 * @param {Array} widgetList 控件列表
 * @param {Array} widgetTypes 要添加的控件类型集
 * @param {Number} pageType 页面类型
 * @returns {
 *   { 是否可添加的状态, 是否可添加的状态文本 }
 *   { status, text }
 * }
 */
const testWidgetTypeAdd = ({ widgetList = [], widgetTypes = [], pageType = 2 }) => {
    let status = true
    let text = ''
    if ((widgetTypes.filter(item => !!item).length > 0) && [1, 3].includes(pageType)) {
        return {
            status: false,
            text: '该页面不能添加组件'
        }
    }

    const widgetListType = [...new Set(widgetList.map(item => item.eles.type))]
    const getStatus = types => widgetTypes.some(item => types.includes(item)) && widgetListType.some(item => types.includes(item))
    if (getStatus(['suspension', 'new-suspension'])) {
        status = false
        text = '页面内只能有一个悬浮窗组件'
    } else if (getStatus(['official-account'])) {
        status = false
        text = '页面内只能有一个公众号关注组件'
    } else if (getStatus(['advertising'])) {
        status = false
        text = '页面内只能有一个广告组件'
    }
    return {
        status,
        text
    }
}

export default testWidgetTypeAdd